package com.yc.spring.jdbc;

import com.google.gson.Gson;
import com.yc.spring.jdbc.bean.Account;
import com.yc.spring.jdbc.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/8/4
 * Email haijunzhou@hnit.edu.cn
 */
@WebServlet("/queryAccount")
public class QueryAccountServlet extends HttpServlet {

//    @Autowired
//    BankBiz bankBiz;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // Web应用上下文对象  => ApplicationContext
        ServletContext servletContext = this.getServletContext();
        ApplicationContext cxt =
                (ApplicationContext) servletContext.getAttribute("cxt");

        AccountDao dao = cxt.getBean(AccountDao.class);
        List<Account> accounts = dao.selectAll();

        Gson gson = new Gson();
        String json = gson.toJson(accounts);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().println(json);
    }
}
